package com.ruoyi.system.controller;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MeetMaterials;
import com.ruoyi.system.service.IMeetMaterialsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议资料管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-09
 */
@RestController
@RequestMapping("/system/meetMaterials")
public class MeetMaterialsController extends BaseController
{
    @Autowired
    private IMeetMaterialsService meetMaterialsService;

    /**
     * 查询会议资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:meetMaterials:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeetMaterials meetMaterials)
    {
        startPage();
        List<MeetMaterials> list = meetMaterialsService.selectMeetMaterialsList(meetMaterials);
        return getDataTable(list);
    }

    /**
     * 导出会议资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:meetMaterials:export')")
    @Log(title = "会议资料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeetMaterials meetMaterials)
    {
        List<MeetMaterials> list = meetMaterialsService.selectMeetMaterialsList(meetMaterials);
        ExcelUtil<MeetMaterials> util = new ExcelUtil<MeetMaterials>(MeetMaterials.class);
        util.exportExcel(response, list, "会议资料管理数据");
    }

    /**
     * 获取会议资料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:meetMaterials:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(meetMaterialsService.selectMeetMaterialsById(id));
    }

    /**
     * 新增会议资料管理
     */
    @PreAuthorize("@ss.hasPermi('system:meetMaterials:add')")
    @Log(title = "会议资料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeetMaterials meetMaterials)
    {
        return toAjax(meetMaterialsService.insertMeetMaterials(meetMaterials));
    }

    /**
     * 修改会议资料管理
     */
    @PreAuthorize("@ss.hasPermi('system:meetMaterials:edit')")
    @Log(title = "会议资料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeetMaterials meetMaterials)
    {
        return toAjax(meetMaterialsService.updateMeetMaterials(meetMaterials));
    }

    /**
     * 删除会议资料管理
     */
    @PreAuthorize("@ss.hasPermi('system:meetMaterials:remove')")
    @Log(title = "会议资料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(meetMaterialsService.deleteMeetMaterialsByIds(ids));
    }

    /**
     * 根据会议ID查询会议资料
     */
//    @PreAuthorize("@ss.hasPermi('system:meetMaterials:query')")
    @GetMapping("/getByMeetId/{meetId}")
    public AjaxResult getByMeetId(@PathVariable("meetId") Long meetId) {
        try {
            MeetMaterials materials = meetMaterialsService.selectMeetMaterialsById(meetId);
            if (materials != null && StringUtils.isNotEmpty(materials.getMinutesPath())) {
                // 完整的文件路径
                String fullPath = RuoYiConfig.getProfile() + materials.getMinutesPath();
                // 读取文件内容
                String content = FileUtils.readFileToString(new File(fullPath), StandardCharsets.UTF_8);
                // 将文件内容添加到返回对象中
                materials.setMinutesContent(content);
            }
            return success(materials);
        } catch (IOException e) {
            return error("读取会议纪要文件失败：" + e.getMessage());
        }
    }
}
